﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SRM.DataObjects;
using System.Data;

public partial class UserControls_ApplicantPhotoUserControl : System.Web.UI.UserControl
{
    public DataTable Photo { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        int applicantKey = int.Parse(Session["ApplicantKey"].ToString());
        this.Photo = SRM.DataObjects.SRM_Photo.GetPhoto(Session["ApplicantKey"].ToString());

        if (!Page.IsPostBack)
        {
            if (this.Photo.Rows.Count > 0)
            {
                this.Photo_FileName.Text = this.Photo.Rows[0]["FileName"].ToString().Trim();

                if (string.IsNullOrEmpty(this.Photo_FileName.Text))
                    this.Photo_FileName.Text = "N/A";
            }
        }
    }
    protected void Upload_Button_Click(object sender, EventArgs e)
    {
        int imageSize;
        string imageType;
        string fileName;
        System.IO.Stream imageStream;

        if (Photo_RadUpload.UploadedFiles.Count > 0)
        {
            imageSize = Photo_RadUpload.UploadedFiles[0].ContentLength;
            imageType = Photo_RadUpload.UploadedFiles[0].ContentType;
            fileName = Photo_RadUpload.UploadedFiles[0].FileName;
            imageStream = Photo_RadUpload.UploadedFiles[0].InputStream;

            byte[] imageContent = new byte[imageSize]; 
            int intStatus;
            intStatus = imageStream.Read(imageContent, 0, imageSize);

            this.Photo = SRM.DataObjects.SRM_Photo.GetPhoto(Session["ApplicantKey"].ToString());
            if (this.Photo.Rows.Count <= 0)
                SRM_Photo.InsertPhoto(Session["ApplicantKey"].ToString(), fileName, imageContent, imageType, imageSize);
            else
                SRM_Photo.UpdatePhoto(Session["ApplicantKey"].ToString(), fileName, imageContent, imageType, imageSize);

            this.Photo_FileName.Text = fileName;
        }
    }

    protected void Download_Button_Click(object sender, EventArgs e)
    {
        System.Data.DataTable dt = SRM_Photo.GetPhoto("756");

    }
}